package com.dm.action;

import com.dm.common.Const;
import com.dm.dto.RecordDto;
import com.dm.dto.ResponseDto;
import com.dm.dto.input.PageDto;
import com.dm.service.RecordService;
import com.dm.utils.JsonRequestDataUtil;
import com.dm.utils.JsonDateValueProcessor;
import com.opensymphony.xwork2.ActionSupport;
import lombok.Data;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * @author hqw521@qq.com
 * @date 2018/11/30 15:36
 */
@Data
@Namespace("/dm/file")
@ParentPackage("json-default")
@Results({@Result(name = "jsonData", type = "json", params = {"root", "jsonData"})})
public class RecordAction extends ActionSupport {
    private String jsonData;
    @Autowired
    private RecordService recordService;

    @Action("findRecords")
    public String findRecords() {
        PageDto pageDto = (PageDto) JsonRequestDataUtil.getBean(ServletActionContext.getRequest(), PageDto.class);
        ResponseDto<List<RecordDto>> responseDtos = recordService.findRecords(pageDto.getCurrentPage(), pageDto.getPageSize());
        JsonConfig jsonConfig = new JsonConfig();
        // 将自定已的date转换器注册进去
        jsonConfig.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor());
        jsonData = JSONObject.fromObject(responseDtos, jsonConfig).toString();
        return Const.JSON;
    }
}
